<template>
    <el-dialog :title="!dataForm.id ? 'Published topics' : 'Revise the topic'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" ref="dataForm" label-width="100px">
            <el-form-item label="scientificName">
                <el-input v-model="dataForm.scientificName" placeholder="Fill in the subject name"></el-input>
            </el-form-item>
            <el-form-item label="Publication name">
                <el-input v-model="dataForm.scientificTitle" placeholder="Fill in the publication name"></el-input>
            </el-form-item>
            <el-form-item label="serial">
                <el-input v-model="dataForm.scientificNumber" placeholder="Fill in the serial number"></el-input>
            </el-form-item>
            <el-form-item label="Upload project files">
                <el-upload
                class="upload-demo"
                :headers="headers"
                :action="uploadUrl"
                :on-success="successHandle"
                :file-list="fileList">
                <el-button size="small" type="primary">Click Upload</el-button>
                <div slot="tip" class="el-upload__tip">Only upload txt,docx,doc,wps,xls,xlsx，only be uploaded once</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="comment">
                <el-input type="textarea" v-model="dataForm.comment"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">Publish Now</el-button>
                <el-button @click="visible = false">cancel</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
  import Vue from 'vue'
  export default {
    data () {
      return {
        uploadUrl: window.SITE_CONFIG['baseUrl'] + '/edu/teduscientific/uploadFile',
        headers: { token: Vue.cookie.get('token') },
        fileList: [],
        visible: false,
        dataForm: {
          scientificName: '',
          scientificTitle: '',
          scientificNumber: '',
          paperAddress: '',
          comment: ''
        }
      }
    },
    methods: {
      init (id) {
        console.log(id)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/edu/teduscientific/queryScientificById/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.data.id
              this.dataForm.scientificName = data.data.scientificName
              this.dataForm.scientificTitle = data.data.scientificTitle
              this.dataForm.scientificNumber = data.data.scientificNumber
              this.dataForm.paperAddress = data.data.paperAddress
              this.dataForm.comment = data.data.comment
            }
          })
        } else {
          this['dataForm'] = {}
        }
      },
      onSubmit () {
        console.log(this.dataForm)
        this.$http({
          url: this.$http.adornUrl(`/edu/teduscientific`),
          method: !this.dataForm.id ? 'post' : 'put',
          data: this.dataForm
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: 'Operation succeeded',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      successHandle (response, file, fileList) {
        console.log(response, file, fileList)
        if (response.code === 0) {
          this.$message.success('success!')
          this.dataForm.paperAddress = response.data.fileAddress
          console.log(this.dataForm)
        }
        if (response.code !== 0) {
          this.$message.error(response.msg)
        }
      }
    }
  }
</script>